package com.eshop.cart.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.SQLException;



import com.eshop.dao.Conn;

public class UpDateCartDao {
	
	
	private Connection conn = null;
	private PreparedStatement sta = null;
	private ResultSet r = null;
	
	public int upDateCart(String goodsAmount, String goodsId)
	{
		int i = 0;
		 conn = Conn.getConn();
		 String sql = "UPDATE eshop.cart SET goods_amount = ? WHERE goods_id = ?";
		 
		 try {
			 
			 
			sta  = conn.prepareStatement(sql);
			sta.setObject(1, goodsAmount);
			sta.setObject(2, goodsId);
			 i = sta.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
			
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
	} catch (SQLException e) {
		e.printStackTrace();
		
	}
}		
		
		return i;

	}

	
	
	public int upDatestock(String goodsAmount, String goodsId)
	{
		
		int i = 0;
		 conn = Conn.getConn();
		 String sql = "UPDATE eshop.goods SET goods_stock = goods_stock-? WHERE goods_id = ?";
		 
		 try {
			 
			 
			sta  = conn.prepareStatement(sql);
			sta.setObject(1, goodsAmount);
			sta.setObject(2, goodsId);
			 i = sta.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
			
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
	} catch (SQLException e) {
		e.printStackTrace();
		
	}
}		
		
		return i;

	}
	
			public int searchPrice(String goodsId)
			{
				 int i = 0;
				 conn = Conn.getConn();
				 String sql = "select goods_price from goods where goods_id = ?";
				 try {
					 
					 
						sta  = conn.prepareStatement(sql);
						sta.setObject(1, goodsId);
						r = sta.executeQuery();
						while(r.next())
						{
							
							i = r.getInt(1);
						}
						
					} catch (SQLException e) {
						
						e.printStackTrace();
					}finally{
						try {
						
							if(sta != null)
							{
								sta.close();
							}
							if(conn != null)
							{
								conn.close();
							}
				} catch (SQLException e) {
					e.printStackTrace();
					
				}
			}		
					
					return i;
					
				
			}
			
	

	
	
	
	
}
